package com.play.ballen.mall.vm

import androidx.lifecycle.MutableLiveData
import com.play.ballen.R
import com.play.ballen.mall.entity.MallBallInfo
import com.play.ballen.mall.entity.MallBallLevel
import com.play.ballen.mall.entity.RateInfo
import com.play.ballen.network.ApiRepository
import com.play.ballen.network.BallenRxHelper
import com.yuven.appframework.busevent.SingleLiveEvent
import com.yuven.appframework.network.RxSchedulerHelper
import com.yuven.appframework.util.addToDisposable
import com.yuven.baselib.component.viewmodel.BaseViewModel
import com.yuven.baselib.utils.Logger
import com.yuven.baselib.utils.ResourceUtil

open class MallVM : BaseViewModel() {

    protected val model = ApiRepository()

    var page = 1
    private val pageSize = 20

    val listBall = MutableLiveData<List<MallBallInfo>?>()
    val listMallBallLevel = MutableLiveData<List<MallBallLevel>?>()
    val actionGetBallFailure = SingleLiveEvent<String>()
    val actionBuySuccess = SingleLiveEvent<Void>()

    val rateInfo = SingleLiveEvent<RateInfo>()

    fun basketballIndex(type: String, levelId: String? = "") {
        val map = mutableMapOf<String, Any>()
        map["page"] = page
        map["pagesize"] = pageSize
        map["type"] = type
        if (!levelId.isNullOrBlank()) {
            map["level_id"] = levelId
        }

        model.basketballIndex(map)
            .onErrorResumeNext(BallenRxHelper.HttpErrorHandleFunc())
            .compose(BallenRxHelper.transformResponse())
            .compose(RxSchedulerHelper.io_main())
            .subscribe({ data ->
                listBall.value = data.list
                listMallBallLevel.value = data.level
                Logger.i("MallVM.basketballIndex success list = $data")
            }, { throwable ->
                actionGetBallFailure.value = throwable.message
                Logger.i("MallVM.basketballIndex error,msg = ${throwable.message}")
            }).addToDisposable(compositeDisposable)
    }

    fun basketballBuy(isOfficial: Boolean, params: MutableMap<String, Any>) {
        showDialog.setValue(true, ResourceUtil.getString(R.string.loading))
        val source = if (isOfficial) {
            model.basketballBuy(params)
        } else {
            model.basketballBuySell(params)
        }
        source.onErrorResumeNext(BallenRxHelper.HttpErrorHandleFunc())
            .compose(BallenRxHelper.transformResponse())
            .compose(RxSchedulerHelper.io_main())
            .doFinally {
                showDialog.setValue(false)
            }
            .subscribe({
                Logger.i("MallVM.basketballBuy success")
                actionBuySuccess.call()
            }, { throwable ->
                toastLiveData.value = throwable.message
                Logger.i("MallVM.basketballBuy error,msg = ${throwable.message}")
            }).addToDisposable(compositeDisposable)
    }

    fun basketballBuyRate() {
        showDialog.setValue(true, ResourceUtil.getString(R.string.loading))
        model.basketballBuyRate()
            .onErrorResumeNext(BallenRxHelper.HttpErrorHandleFunc())
            .compose(BallenRxHelper.transformResponse())
            .compose(RxSchedulerHelper.io_main())
            .doFinally {
                showDialog.setValue(false)
            }
            .subscribe({
                Logger.i("MallVM.basketballBuyRate success")
                rateInfo.value = it
            }, { throwable ->
                toastLiveData.value = throwable.message
                Logger.i("MallVM.basketballBuyRate error,msg = ${throwable.message}")
            }).addToDisposable(compositeDisposable)
    }

}